package com.lancoo.ccas53.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas53.aspect.LogAnnotation;
import com.lancoo.ccas53.entity.College;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.dto.TeachingClassWeekScheduleDto;
import com.lancoo.ccas53.pojo.vo.AllScheduleVo;
import com.lancoo.ccas53.pojo.vo.RoomVo;
import com.lancoo.ccas53.pojo.vo.StudentCollegeTree;
import com.lancoo.ccas53.pojo.vo.TeachingClassSegmentVo;
import com.lancoo.ccas53.service.CollegeService;
import com.lancoo.ccas53.service.ScheduleService;
import com.lancoo.ccas53.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: liufeng
 * @CreateTime: 2024-07-17  21:50
 * @Description:
 */
@Api(tags = "课表")
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private CollegeService collegeService;

    @ApiOperation("修改课表周次")
    @PostMapping(value = "/updateWeek")
    @LogAnnotation(operationType = "2", operationContent = "修改课表周次")
    public ResponseObject<Object> updateWeek(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                             @ApiParam(value = "week", required = true) @RequestParam @NotEmpty String week,
                                             @ApiParam(value = "课表id", required = true) @RequestBody List<Long> ids) {
        return ResponseObject.ofSuccess(scheduleService.updateWeek(taskId, ids, week));
    }

    @ApiOperation("拆分课表")
    @PostMapping(value = "/subSchedule")
    @LogAnnotation(operationType = "2", operationContent = "拆分课表")
    public ResponseObject<Object> subSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                             @ApiParam(value = "课表id", required = true) @RequestBody List<Long> ids) {
        return ResponseObject.ofSuccess(scheduleService.subSchedule(taskId, ids));
    }

    @ApiOperation("获取教师课表")
    @GetMapping(value = "/getTeacherSchedule")
    @LogAnnotation(operationType = "3", operationContent = "获取教师课表")
    public ResponseObject<List<TeachingClassWeekScheduleDto>> getTeacherSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                 @ApiParam(value = "教师id", required = true) @RequestParam Long teacherId,
                                                                                 @ApiParam(value = "timeCode", required = false) @RequestParam(required = false) String timeCodes) {
        return ResponseObject.ofSuccess(scheduleService.getTeacherSchedule(taskId, teacherId, timeCodes));
    }

    @ApiOperation("获取教室树")
    @GetMapping(value = "/getRoomTree")
    @LogAnnotation(operationType = "3", operationContent = "获取全部教师 根据课题组分组")
    public ResponseObject<List<RoomVo>> getRoomTree(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                    @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                    @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                    @ApiParam(value = "module 1：排课状态 2：不要排课状态", required = true) @RequestParam Integer module,
                                                    @ApiParam(value = "baseCollegeId", required = false) @RequestParam(required = false) String baseCollegeId,
                                                    @ApiParam(value = "keyword", required = false) @RequestParam(required = false) String keyword) {
        return ResponseObject.ofSuccess(scheduleService.getRoomTree(taskId, schoolId, term, baseCollegeId, module, keyword));
    }


    @ApiOperation("获取教室课表")
    @GetMapping(value = "/getRoomSchedule")
    @LogAnnotation(operationType = "3", operationContent = "获取教室课表")
    public ResponseObject<List<TeachingClassWeekScheduleDto>> getRoomSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                              @ApiParam(value = "教室id", required = true) @RequestParam Long roomId,
                                                                              @ApiParam(value = "timeCode", required = false) @RequestParam(required = false) String timeCodes) {
        return ResponseObject.ofSuccess(scheduleService.getRoomSchedule(taskId, roomId, timeCodes));
    }

    @ApiOperation("获取剩余可排课教室信息")
    @GetMapping(value = "/exportTimeSlotRoom")
    @LogAnnotation(operationType = "14", operationContent = "获取剩余可排课教室信息")
    public ResponseObject<Object> exportTimeSlotRoom(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                     @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                     @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                     @ApiParam(value = "返回响应", required = false) HttpServletResponse response) {
        scheduleService.exportTimeSlotRoom(taskId, schoolId, term, response);
        return ResponseObject.ofSuccess();
    }

    @ApiOperation("获取学生树")
    @GetMapping(value = "/getStudentTree")
    @LogAnnotation(operationType = "3", operationContent = "获取教室课表")
    public ResponseObject<List<StudentCollegeTree>> getStudentTree(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                   @ApiParam(value = "keyword", required = false) @RequestParam(required = false) String keyword) {
        return ResponseObject.ofSuccess(scheduleService.getStudentTree(taskId, keyword));
    }

    @ApiOperation("获取学生课表")
    @GetMapping(value = "/getStudentSchedule")
    @LogAnnotation(operationType = "3", operationContent = "获取学生课表")
    public ResponseObject<List<TeachingClassWeekScheduleDto>> getStudentSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                 @ApiParam(value = "学生id", required = true) @RequestParam Long studentId) {
        return ResponseObject.ofSuccess(scheduleService.getStudentSchedule(taskId, studentId));
    }

    @ApiOperation("获取行政班树")
    @GetMapping(value = "/getClassTree")
    @LogAnnotation(operationType = "3", operationContent = "获取行政班树")
    public ResponseObject<List<StudentCollegeTree>> getClassTree(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "keyword", required = false) @RequestParam(required = false) String keyword) {
        return ResponseObject.ofSuccess(scheduleService.getClassTree(taskId, keyword));
    }

    @ApiOperation("获取行政班课表")
    @GetMapping(value = "/getClassSchedule")
    @LogAnnotation(operationType = "3", operationContent = "获取行政班课表")
    public ResponseObject<List<TeachingClassWeekScheduleDto>> getClassSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                               @ApiParam(value = "行政班id", required = true) @RequestParam String classId) {
        return ResponseObject.ofSuccess(scheduleService.getClassSchedule(taskId, classId));
    }

    @ApiOperation("获取全校教师总表")
    @GetMapping(value = "/allTeacher")
    @LogAnnotation(operationType = "3", operationContent = "查看全校教师总表")
    public ResponseObject<PageInfo<AllScheduleVo>> getAllTeacherSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                         @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                                                                         @ApiParam(value = "学期", required = false) @RequestParam(required = false) String term,
                                                                         @ApiParam(value = "教师名称关键字", required = false) @RequestParam(required = false) String keyword,
                                                                         @ApiParam(value = "单位", required = false) @RequestParam(required = false) String collegeId,
                                                                         @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                         @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(scheduleService.getAllTeacherSchedule(taskId, schoolId, keyword, term, collegeId, currentPage, pageSize));
    }

    @ApiOperation("获取全校教室总表")
    @GetMapping(value = "/allRoom")
    @LogAnnotation(operationType = "3", operationContent = "查看全校教室总表")
    public ResponseObject<PageInfo<AllScheduleVo>> getAllRoomSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                      @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                                                                      @ApiParam(value = "学期", required = false) @RequestParam(required = false) String term,
                                                                      @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId,
                                                                      @ApiParam(value = "建筑id", required = false) @RequestParam(required = false) Integer buildingId,
                                                                      @ApiParam(value = "教室名称关键字", required = false) @RequestParam(required = false) String keyword,
                                                                      @ApiParam(value = "单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                      @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                      @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(scheduleService.getAllRoomSchedule(taskId, schoolId, term, campusId, buildingId, keyword, currentPage, pageSize));
    }

    @ApiOperation("获取全校学生总表")
    @GetMapping(value = "/allStudent")
    @LogAnnotation(operationType = "3", operationContent = "获取全校学生总表")
    public ResponseObject<PageInfo<AllScheduleVo>> getAllStudentSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                         @ApiParam(value = "学校id", required = false) @RequestParam(required = false) String schoolId,
                                                                         @ApiParam(value = "学期", required = false) @RequestParam(required = false) String term,
                                                                         @ApiParam(value = "年级id", required = false) @RequestParam(required = false) String gradeId,
                                                                         @ApiParam(value = "班级", required = false) @RequestParam(required = false) String classId,
                                                                         @ApiParam(value = "教师名称关键字", required = false) @RequestParam(required = false) String keyword,
                                                                         @ApiParam(value = "单位", required = false) @RequestParam(required = false) String collegeId,
                                                                         @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                         @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(scheduleService.getAllStudentSchedule(taskId, schoolId, term, gradeId, classId, keyword, collegeId, currentPage, pageSize));
    }

    @ApiOperation("获取环节教学班列表")
    @GetMapping(value = "/segment")
    @LogAnnotation(operationType = "3", operationContent = "查看环节教学班列表")
    public ResponseObject<PageInfo<TeachingClassSegmentVo>> getTeachingClassSegment(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                    @ApiParam(value = "年级", required = false) @RequestParam(required = false) String gradeId,
                                                                                    @ApiParam(value = "专业", required = false) @RequestParam(required = false) String majorId,
                                                                                    @ApiParam(value = "环节类型", required = false) @RequestParam(required = false) String type,
                                                                                    @ApiParam(value = "搜索关键字", required = false) @RequestParam(required = false) String keyword,
                                                                                    @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize,
                                                                                    @ApiParam(value = "单位", required = false) @RequestParam(required = false) String baseCollegeId) {
        Page<TeachingClassSegmentVo> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(scheduleService.getTeachingClassSegmentPage(taskId, page, gradeId, majorId, type, keyword, baseCollegeId));
    }

    @ApiOperation("查看提交")
    @GetMapping(value = "/commit")
    @LogAnnotation(operationType = "3", operationContent = "查看提交")
    public ResponseObject<PageInfo<College>> commit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                    @ApiParam(value = "单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                    @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                    @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<College> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(collegeService.getScheduleCommit(page, taskId, baseCollegeId));
    }

    @ApiOperation("提交")
    @PostMapping(value = "/commit")
    @LogAnnotation(operationType = "5", operationContent = "锁定all")
    public ResponseObject<Object> commit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                         @ApiParam(value = "用户id", required = true) @RequestHeader String userId,
                                         @ApiParam(value = "用户名称", required = true) @RequestHeader String userName,
                                         @ApiParam(value = "解锁 1，锁定 2", required = true) @RequestParam Integer lock,
                                         @ApiParam(value = "第几轮次", required = false) @RequestParam(required = false) Integer arrangeRound,
                                         @ApiParam(value = "单位", required = false) @RequestParam(required = false) String baseCollegeId) throws UnsupportedEncodingException {
        collegeService.scheduleCommit(taskId, baseCollegeId, userId, userName, lock, arrangeRound);
        return ResponseObject.ofSuccess();
    }

    @ApiOperation("锁定轮次")
    @GetMapping(value = "/lockRound")
    @LogAnnotation(operationType = "3", operationContent = "获取轮次")
    public ResponseObject<Object> lockRound(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(scheduleService.lockRound(taskId));
    }
}
